package com.github.wui.chart;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.github.logger.LoggerKt;
import com.github.wui.R;
import com.github.wui.chart.chartDataSet.LineChartDataSet;
import com.github.wui.circle.CircleLinearLayout;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * 自定义一个图表控件
 * 属性
 * <pre>
 *  <code >line_color</code>
 *  <code >line_to_text_space</code>
 *  <code >lineChartView_badge_color</code>
 *  <code>lineChartView_badge_textSize</code>
 *  <code>lineChartView_showBadge</code>
 *  <code >lineChartView_lineBarWidth</code>
 *  <code >lineChartView_labelSize</code>
 * </pre>
 *
 * @author Mr.xie
 */
public class LineChartView extends CircleLinearLayout {


    /**
     * 数据集合
     */
    private List<LineChartDataSet> dataSets = new ArrayList<>();

    private float lineChartWidth = 20f;

    private BadgeStyle badgeStyle = new BadgeStyle(10, Color.GRAY);

    private int label_textSize = 12;


    private int labelToLineSpace = 10;

    private boolean isShowBadge = true;

    private int line_color = Color.BLACK;

    /**
     * 逻辑最大值
     */
    private double logicMaxValue = 0;

    public LineChartView(Context context) {
        this(context, null);
    }

    public LineChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.LineChartView);
        lineChartWidth = typedArray.getFloat(R.styleable.LineChartView_lineChartView_lineBarWidth, 20f);
        badgeStyle.color = typedArray.getColor(R.styleable.LineChartView_lineChartView_badge_color, Color.GRAY);
        badgeStyle.fontSize = typedArray.getDimensionPixelSize(R.styleable.LineChartView_lineChartView_badge_textSize, 10);
        label_textSize = typedArray.getDimensionPixelSize(R.styleable.LineChartView_lineChartView_labelSize, 12);
        labelToLineSpace = typedArray.getInt(R.styleable.LineChartView_lineChartView_line_to_text_space, 10);
        isShowBadge = typedArray.getBoolean(R.styleable.LineChartView_lineChartView_showBadge, true);
        line_color = typedArray.getColor(R.styleable.LineChartView_lineChartView_lineColor, Color.BLACK);
        typedArray.recycle();
    }


    /**
     * 设置 线性柱状图的数据集
     *
     * @param dataSets 数据集
     */
    public void setLineDataSet(List<LineChartDataSet> dataSets) {
        if (getChildCount()>0) {
            removeAllViews();
        }
        this.dataSets.clear();;
        this.dataSets = dataSets;
        postInvalidate();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        int h = MeasureSpec.getSize(heightMeasureSpec);
        int w = MeasureSpec.getSize(widthMeasureSpec);
        if (layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) {
            h = 400;
        }

        if (layoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT) {
            w = 400;
        }

        if (layoutParams.width != ViewGroup.LayoutParams.WRAP_CONTENT && layoutParams.width != ViewGroup.LayoutParams.MATCH_PARENT){
            w = layoutParams.width;
        }

        if (layoutParams.height != ViewGroup.LayoutParams.WRAP_CONTENT && layoutParams.width != ViewGroup.LayoutParams.MATCH_PARENT){
            h = layoutParams.height;
        }

        setMeasuredDimension(w + (getPaddingLeft() + getPaddingRight()),h + getPaddingTop() + getPaddingBottom());
        initDataSetView();
    }

    private void initDataSetView() {
        int viewHeight = getMeasuredHeight();
        logicMaxValue = dataSets.stream().map(LineChartDataSet::getValue).max(new Comparator<Double>() {
            @Override
            public int compare(Double o1, Double o2) {
                return o1.compareTo(o2);
            }
        }).get();
        LoggerKt.d("lineDataSet height === " + viewHeight + " logicMaxValue  : " + logicMaxValue);
        for (int i = 0; i < this.dataSets.size(); i++) {
            LineChartDataSet lineChartDataSet = dataSets.get(i);
            CircleLinearLayout chartItemView = new CircleLinearLayout(getContext());
            chartItemView.setOrientation(VERTICAL);

            // 创建数字badge文本
            TextView badgeTextView = new TextView(getContext());
            badgeTextView.setTextColor(badgeStyle.color);
            badgeTextView.setTextSize(badgeStyle.fontSize);
            badgeTextView.setText(String.valueOf(lineChartDataSet.getValue()));
            badgeTextView.setTextAlignment(TEXT_ALIGNMENT_CENTER);

            // 创建线性图条
            CircleLinearLayout chartLineView = new CircleLinearLayout(getContext());
            // 文本标签
            TextView tv = new TextView(getContext());
            tv.setText(lineChartDataSet.getLabel());
            tv.setTextSize(label_textSize);
            tv.setTextAlignment(TEXT_ALIGNMENT_CENTER);

            // 线性图条 和 文字标签的距离
            View VspanView = new View(getContext());
            VspanView.setLayoutParams(new MarginLayoutParams(0, labelToLineSpace));
            // 设置线性图条的样式
            MarginLayoutParams chartLineViewParams = new MarginLayoutParams((int) lineChartWidth, (int) (((getMeasuredHeight() - 130 - VspanView.getMeasuredHeight()) * lineChartDataSet.getValue()) / logicMaxValue));
            chartLineView.setLayoutParams(chartLineViewParams);
            chartLineView.setRound(lineChartWidth);
            chartLineView.setBkColor(this.dataSets == null || this.dataSets.isEmpty() || lineChartDataSet.getColor() == 0 ? line_color : lineChartDataSet.getColor());
            //设置整个图标的参数
            MarginLayoutParams marginLayoutParams = new MarginLayoutParams((getMeasuredWidth() - getPaddingLeft() - getPaddingRight()) / dataSets.size(), ViewGroup.LayoutParams.WRAP_CONTENT);
            chartItemView.setLayoutParams(marginLayoutParams);
            chartItemView.setGravity(Gravity.CENTER);
            chartItemView.setBkColor(getBkColor());
            if (isShowBadge){
                chartItemView.addView(badgeTextView);
            }
            chartItemView.addView(chartLineView);
            chartItemView.addView(VspanView);
            chartItemView.addView(tv);
            addView(chartItemView);
        }
    }


    public List<LineChartDataSet> getLineDataSets() {
        return dataSets;
    }
    public float getLineChartWidth() {
        return lineChartWidth;
    }

    public void setLineChartWidth(float lineChartWidth) {
        this.lineChartWidth = lineChartWidth;
        postInvalidate();
    }

    public BadgeStyle getBadgeStyle() {
        return badgeStyle;
    }

    public void setBadgeStyle(BadgeStyle badgeStyle) {
        this.badgeStyle = badgeStyle;
        postInvalidate();
    }

    public int getLabel_textSize() {
        return label_textSize;
    }

    public void setLabel_textSize(int label_textSize) {
        this.label_textSize = label_textSize;
        postInvalidate();
    }

    public int getLabelToLineSpace() {
        return labelToLineSpace;
    }

    public void setLabelToLineSpace(int labelToLineSpace) {
        this.labelToLineSpace = labelToLineSpace;
        postInvalidate();
    }

    public boolean isShowBadge() {
        return isShowBadge;
    }

    public void setShowBadge(boolean showBadge) {
        isShowBadge = showBadge;
        postInvalidate();
    }

    public int getLine_color() {
        return line_color;
    }

    public void setLine_color(int line_color) {
        this.line_color = line_color;
        postInvalidate();
    }

    public double getLogicMaxValue() {
        return logicMaxValue;
    }

    public void setLogicMaxValue(double logicMaxValue) {
        this.logicMaxValue = logicMaxValue;
        postInvalidate();
    }

    public class BadgeStyle {
        public int fontSize = 10;
        public int color = Color.GRAY;

        public BadgeStyle(int fontSize, int color) {
            this.fontSize = fontSize;
            this.color = color;
        }

        public int getFontSize() {
            return fontSize;
        }

        public void setFontSize(int fontSize) {
            this.fontSize = fontSize;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }
    }
}
